/*******************************************************************************
 * Copyright (c) 2010 L.Carbonnaux.
 *******************************************************************************/

package org.lcx.taskvision.core;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import com.google.gdata.client.spreadsheet.FeedURLFactory;

/**
 * @author Laurent Carbonnaux
 */
public class TaskVisionCorePlugin extends AbstractUIPlugin {

	// log for development
	public static final boolean isLogEnable = false;
	
	// The plug-in ID
	public static final String PLUGIN_ID = "org.lcx.taskvision.core";

	public final static String CONNECTOR_KIND = Properties.ScrumVisionConnectorKind;

	public final static String CLIENT_LABEL = Messages.ScrumVisionCorePlugin_client_label;

	public final static String TITLE_MESSAGE_DIALOG = Messages.ScrumVisionCorePlugin_title_message_dialog;
	
	// The shared instance
	private static TaskVisionCorePlugin plugin;
	
	private static TaskVisionRepositoryConnector connector;

	
	private static TaskVisionClientManager clientManager;
	public static TaskVisionClientManager getClientManager() {
		if (clientManager == null) {
			clientManager = new TaskVisionClientManager();
		}
		return clientManager;
	}
	
	
	/** A factory that generates the appropriate feed URLs. */
	private static FeedURLFactory feedURLFactory;
	
	public static FeedURLFactory getFeedURLFactory() {
		if(feedURLFactory==null){
			feedURLFactory = FeedURLFactory.getDefault();
		}
		return feedURLFactory;
	}
	
	public static String getSpreadsheetsFeedUrl(){
		return getFeedURLFactory().getSpreadsheetsFeedUrl().toString();
	}

	/**
	 * The constructor
	 */
	public TaskVisionCorePlugin() {
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
		plugin.getPreferenceStore().setDefault(ITaskVisionConstants.PREF_UPDATE_ALL_TASK, Boolean.TRUE);
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static TaskVisionCorePlugin getDefault() {
		return plugin;
	}

	public static TaskVisionRepositoryConnector getConnector(TaskRepository taskRepository) {
		if(connector==null) {
			connector = new TaskVisionRepositoryConnector();
		}
		return connector;
	}

	/**
	 * Convenience method for logging statuses to the plugin log
	 * 
	 * @param status
	 *            the status to log
	 */
	public static void log(IStatus status) {
		if(isLogEnable) getDefault().getLog().log(status);
	}

	/**
	 * Convenience method for logging exceptions to the plugin log
	 * 
	 * @param e
	 *            the exception to log
	 */
	public static void logError(Throwable e) {
		String message = e.getMessage();
		if (message == null) {
			message = e.getClass().toString();
		}
		getDefault().getLog().log(new Status(Status.ERROR, TaskVisionCorePlugin.PLUGIN_ID, 0, message, e));
	}
	
}
